#include<bits/stdc++.h>
#define int long long
#define N 400005
using namespace std;
int n;
int fa[N],a[N],w[N];
int ans;
int Find(int x){return x==fa[x]?x:fa[x]=Find(fa[x]);}
signed main(){
    freopen("pairint.in","r",stdin);
    freopen("pairint.out","w",stdout);
    scanf("%lld",&n);
    for(int i=1;i<=2*n;i++) fa[i]=i,a[i]=1;
    for(int i=1,u,v;i<=n;i++){
        scanf("%lld %lld",&u,&v);
        u=Find(u);v=Find(v);
        if(u==v){w[u]=1;continue;}
        a[u]+=a[v];
        w[u]|=w[v];
        fa[v]=u;
    }
    for(int i=1;i<=2*n;i++) if(i==Find(i)) ans+=a[i]-1+w[i];
    printf("%lld\n",ans);
    return 0;
}